package main.kotlin.com.learn.step06_string


const val TAVERN_NAME = "Taernyl's Folly"

fun main(args: Array<String>) {


    placeOrder()

    placeOrder("shandy,Dragon's Breath,5.91")

    val phrase = "Ah, delicious $TAVERN_NAME!"
    println("Madrigal exclaims: ${toDragonSpeak(phrase)}")
    println("Madrigal exclaims: ${toDragonSpeak2(phrase)}")

    println("helloL".replace("l", "x"))
    // 忽略大小写
    println("helloL".replace("l", "x", true))

    println("hello" == "hello")

    val unicodeCapitalA: Char = '\u0041'
    println(unicodeCapitalA)
    // A

    val capitalA = 97;
    println(capitalA.toChar())
    // a

    val omSymbol = '\u0950'
    println(omSymbol)
    // ॐ

    // -- 遍历字符
    "hello world".forEach { char -> println(char) }
}

private fun toDragonSpeak(phrase: String) =
    phrase.replace(Regex("[aeiou]")) {
        when (it.value) {
            "a" -> "4"
            "e" -> "3"
            "i" -> "2"
            "o" -> "1"
            "u" -> "0"
            else -> it.value
        }
    }

private fun toDragonSpeak2(phrase: String): String {
    return phrase.replace(Regex("[aeiou]"), { it ->
        when (it.value) {
            "a" -> "4"
            "e" -> "3"
            "i" -> "2"
            "o" -> "1"
            "u" -> "0"
            else -> it.value
        }
    })
}


private fun placeOrder(menuData: String) {
//    val data = menuData.split(',');
//    val type = data[0]
//    val name = data[1]
//    val price = data[2]
//    val message = "Madrigal buys a $name ($type) for $price."
//    println(message)


    val (type, name, price) = menuData.split(',')
    val message = "Madrigal buys a $name ($type) for $price."
    println(message)
}

private fun placeOrder() {
    val indexOfApostrophe = TAVERN_NAME.indexOf('\'')
    val tavernMaster = TAVERN_NAME.substring(0 until indexOfApostrophe)
    println(tavernMaster)
}